package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.pojo.Menu;
import com.xrit.sunnyday.pojo.Role;
import com.xrit.sunnyday.service.MenuService;
import com.xrit.sunnyday.common.MessageConstant;
import com.xrit.sunnyday.common.Result;
import com.xrit.sunnyday.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RequestMapping("/menu")
@RestController
public class MenuController {
    /*
    远程调用
     */
    @Reference
    private MenuService menuService;
    @Reference
    private UserService userService;
    /*
    查询菜单
     */
    @RequestMapping("/findMenu.do")
    public Result findMenu(){
        //通过springsecurity的方法去获取用户名称
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = user.getUsername();
        //根据用户名称查询用户信息
        com.xrit.sunnyday.pojo.User userInDb = userService.findByUserName(username);
        //循环遍历userInDb中的角色
        Set<Role> roles = userInDb.getRoles();
        List<Menu> menuList=null;
        for (Role role : roles) {
            //调用根据角色id查询相关菜单的方法
            menuList=menuService.findMenu(role.getId());
        }
         return new Result(true,MessageConstant.QUERY_MENU_SUCCESS,menuList);
    }
}

